package com.app.micaihu.view.login.activity

import android.os.Bundle
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.bean.infor.UserInfor
import com.app.micaihu.configure.Config
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.view.bean.AccountConflict
import com.app.micaihu.view.login.contract.LogInContract
import com.app.micaihu.view.login.dialog.AccountConflictDialog
import com.app.micaihu.view.login.presenter.LogInPresenter
import com.app.micaihu.view.login.sy.ConfigUtils
import com.app.micaihu.view.login.utils.CloseLoginActivityUtils
import com.blankj.utilcode.util.JsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.ToastUtils
import com.chuanglan.shanyan_sdk.OneKeyLoginManager
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.login_first_activity.*

/**
 * Created by Guo on 2016/7/24.
 * 绑定手机
 */
class BindPhoneActivity : BaseMvpActivity<LogInContract.View, LogInPresenter>(), LogInContract.View {

    override fun getPresenter() = LogInPresenter()

    override fun getLayoutId() = R.layout.login_first_activity

    override fun getBundle(bundle: Bundle) {
        showSkip = bundle.getBoolean(Config.Extra.showSkip)
    }

    private var showSkip: Boolean = false

    override fun setData() {
        OneKeyLoginManager.getInstance().setAuthThemeConfig(ConfigUtils.getBindPhoneCJSConfig(thisActivity, showSkip), null)
        openLoginAuth()
    }


    override fun addListeners() {

    }

    /**
     * 拉取授权页方法
     */
    private fun openLoginAuth() {
        OneKeyLoginManager.getInstance().openLoginAuth(false, { code, result ->
            if (1000 == code) {
//                    isOpenAuth = true;
                //拉起授权页成功
                LogUtils.e("VVV", "拉起授权页成功： _code==$code   _result==$result")
            } else {
                LogUtils.e("VVV", "拉起授权页失败： _code==$code   _result==$result")
                OneKeyLoginManager.getInstance().finishAuthActivity()
                OneKeyLoginManager.getInstance().removeAllListener()
                enterPager(BindPhoneByCodeActivity::class.java)
                progressBar.postDelayed({ finish() }, 200)
            }
        }) { code, result ->
            if (1011 == code) {
//                    isOpenAuth = false;
                LogUtils.e("VVV", "用户点击授权页返回： _code==$code   _result==$result")
                finish()
            } else if (1000 == code) {
                LogUtils.e("VVV", "用户点击登录获取token成功： _code==$code   _result==$result")
                val token = JsonUtils.getString(result, "token")
                LogUtils.e("token->$token")
                if (StringUtils.isEmpty(token)) {
                    enterPager(BindPhoneByCodeActivity::class.java)
                } else {
                    mPresenter?.flashBoundMobile(token)
                }
            } else {
                LogUtils.e("VVV", "用户点击登录获取token失败： _code==$code   _result==$result")
                enterPager(BindPhoneByCodeActivity::class.java)
                progressBar.postDelayed({ finish() }, 200)
            }
            OneKeyLoginManager.getInstance().finishAuthActivity()
            OneKeyLoginManager.getInstance().removeAllListener()
        }
    }

    override fun handleFlashBoundMobile(result: BaseBean<AccountConflict?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                CloseLoginActivityUtils.closeAllBindPhoneActivity()
            } else {
                val data = result.data
                if (data == null) {
                    ToastUtils.showShort(result.msg)
                } else {
                    val dialog = AccountConflictDialog(thisActivity, data)
                    dialog.show()
                    dialog.setOnClickListener(object : AccountConflictDialog.OnClickListener {

                        override fun onOk(currentSelect: Boolean) {
                            var masterUid = ""
                            var slaveUid = ""

                            if (currentSelect) {
                                data.currentUser?.let {
                                    masterUid = it.uid
                                }
                                data.clashUser?.let {
                                    slaveUid = it.uid
                                }
                            } else {
                                data.currentUser?.let {
                                    slaveUid = it.uid
                                }
                                data.clashUser?.let {
                                    masterUid = it.uid
                                }
                            }
                            mPresenter?.setMainUser(masterUid, slaveUid)
                        }

                        override fun onCancel() {
                            finish()
                        }

                    })
                }
            }
        }
    }

    override fun handleSetMainUser(result: BaseBean<UserInfor?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
            CloseLoginActivityUtils.closeAllBindPhoneActivity()
        } else {
            if (result.isSuccess()) {
                result.data?.let { data ->
                    //保存到本地
                    UserInforManager.getInstance().saveUserInfor(data)
                    //拉取任务数据
                    UserInforManager.getInstance().getUserTask(this, true)
                    LiveEventBus.get(Config.EventBus.loginSuccess).post(true)
                }
                CloseLoginActivityUtils.closeAllBindPhoneActivity()
            }
            ToastUtils.showShort(result.msg)
        }
    }
}